<template>
  <div class="app-container">
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <el-input
          v-model="query.address"
          clearable
          size="small"
          placeholder="请输入智能合约地址（用户地址）"
          style="width: 300px"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
        <!-- <el-button type="primary" @click="downloadFile">数据导出</el-button> -->
        <el-button type="primary" class="filter-item" size="mini" @click="$duduxia({...crud.query,page:1,size:99999999},'/export/leaderboard',$route.meta.title)">数据导出</el-button>

      </div>
      <crudOperation :permission="permission" />

    </div>
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      style="width: 100%"
      stripe
      :row-key="getRowKeys"
      @selection-change="selectionChange"
    >

      <el-table-column
        v-if="columns.visible('address')"
        :show-overflow-tooltip="true"
        prop="address"
        label="用户地址"
      />
      <el-table-column
        v-if="columns.visible('invites')"
        :show-overflow-tooltip="true"
        prop="invites"
        label="直推好友数量"
      />

      <el-table-column
        v-if="columns.visible('rowNum')"
        prop="rowNum"
        label="排名"
      >
        <!-- <template slot-scope="scope">{{
          scope.$index+1
        }}</template> -->
      </el-table-column>
      <!-- createAt -->
      <!-- <el-table-column
          v-if="columns.visible('createTime')"
          prop="createTime"
          :show-overflow-tooltip="true"
          label="注册时间"
        >
          <template slot-scope="scope">
            <div>
              {{ $moment(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss") }}
            </div>
          </template>
        </el-table-column> -->

    </el-table>
    <!--分页组件-->
    <pagination />

    <!-- 充值弹窗 -->
    <el-dialog
      title="充值"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="payform"
        :model="payform"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="数量" prop="amount">
          <el-input v-model="payform.amount" style="width: 75%" />
        </el-form-item>
        <!-- <el-form-item label="账户类型" prop="assetType">
            <el-select v-model="payform.assetType" placeholder="请选择币种">
              <el-option label="交易" :value="1" />
              <el-option label="节点" :value="2" />
              <el-option label="BOK" :value="2" />

            </el-select>
          </el-form-item>
           -->
        <el-form-item label="选择币种" prop="coinId1">
          <el-select v-model="payform.coinId1" placeholder="请选择币种" @change="">
            <el-option label="USDT" :value="1" />
            <el-option label="交易BOK" :value="2" />
            <el-option label="节点BOK" :value="3" />

          </el-select>
        </el-form-item>

        <!-- <el-form-item label="备注">
            <el-input type="textarea" v-model="payform.remake"></el-input>
          </el-form-item> -->
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit('payform')">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import customerCrudMethods from '@/api/customer'
import { userCharge } from '@/api/customer'
import CRUD, { presenter, header, crud, form } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { downloadFile } from '../../utils'
import { exportCustomerExcel } from '../../api/exportApi'
// crud交由presenter持有
const defaultCrud = CRUD({
  url: '/admin/user/sports',
  pageNum: '10',
  title: '用户',
  crudMethod: { ...customerCrudMethods }
})
const defaultForm = {
  delFlag: null,
  uid: null,
  inviteCode: null,
  param: 0
}
export default {
  name: 'Customer',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  data() {
    return {
      params: {

      },
      permission: {
        view: ['admin', 'customer:view'],
        recharge: ['admin', 'customer:recharge'],
        export: ['admin', 'customer:export'],
        add: ['admin'],
        edit: ['admin'],
        del: ['admin']
      },
      dialogVisible: false,
      giftList: [],
      payform: {
        userId: null,
        coinId1: null,
        amount: null
      },
      rules: {
        amount: [
          { required: true, message: '请输入充值数量', trigger: 'blur' },
          {
            pattern: /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/,
            message: '请输入正数',
            trigger: 'blur'
          }
        ],
        coinId1: [{ required: true, message: '请选择币种', trigger: 'change' }],
        assetType: [{ required: true, message: '请选择账户类型', trigger: 'change' }]
      },
      check: true,
      pickerDisabled: {
        disabledDate: (time) => {
          return time.getTime() > new Date().getTime()
        }
      }
    }
  },
  created() {},
  methods: {
    [CRUD.HOOK.beforeReset]() {
      // pageNum:1,
      //   pageSize:10,
      // this.params = {

      // }
    },
    [CRUD.HOOK.beforeRefresh](crud) {
      const { date } = this.params
      crud.query.endDate = ''
      if (date) {
        const [startDate, endDate] = date
        crud.query.startDate = startDate
        crud.query.endDate = endDate
      } else {
        crud.query.startDate = ''
        crud.query.endDate = ''
      }
    },
    [CRUD.HOOK.beforeToEdit](crud) {
      crud.form.parentAccount = crud.form.parentPhone
    },
    handleClose(done) {
      this.dialogVisible = false
    },
    openDialog(data) {
      this.dialogVisible = true
      this.payform.userId = data.userId
      this.payform.amount = null
      this.payform.coinId1 = null
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            ...this.payform
          }
          params.coinId1 == 1 ? params.coinId = 1 : params.coinId = 2
          params.coinId1 == 3 ? params.assetType = 1 : params.assetType = 0
          // console.log(params)
          //   return;
          userCharge(params).then(res => {
            if (res.code == 0) {
              this.$message.success('充值成功')
              this.dialogVisible = false
              this.crud.toQuery()
            } else {
              this.$message.error(res.message)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    goDetail({ userId }) {
      this.$router.push({
        path: '/customer/detail',
        query: { userId }
      })
    },
    getRowKeys(row) {
      return row.uuid // 每条数据的唯一识别值
    },
    selectionChange(val) {
      this.giftList = val
      console.log(this.giftList)
      if (val.length > 0) {
        this.check = false
      } else {
        this.check = true
      }
    },
    downloadFile() {
      exportCustomerExcel({
        ...this.crud.query
      }).then((res) => {
        downloadFile(res.data, '用户', 'xlsx')
      })
    }
  }
}
</script>

  <style lang="scss" scoped>
  .red {
    color: red;
  }
  </style>
